package cn.tedu.charging.common.utils;

import com.fasterxml.jackson.databind.ObjectMapper;

import java.util.LinkedHashMap;

public class JsonUtils {
    //定义对象转换工具ObjectMapper jackson
    private static ObjectMapper objectMapper = new ObjectMapper();

    /**
     * 对象转json
     * @param object
     * @return
     */
    public static String toJson(Object object) {
        try {
            return objectMapper.writeValueAsString(object);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }
    /**
     * json转对象
     * @param json
     * @param clazz
     * @return
     */
    public static <T> T fromJson(String json, Class<T> clazz) {
        try {
            return objectMapper.readValue(json, clazz);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    public static <T> T convertValue(Object object, Class<T> clazz) {
        return objectMapper.convertValue(object,clazz);
    }
}
